package edu.nus.iss.t5.jass.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

import edu.nus.iss.t5.jass.controller.JobAdvertisementController;
import edu.nus.iss.t5.jass.entity.JobAdvertisement;

/**
 * This class create Update Panel for creating Job Adv.
 * @author Team
 */

public class UpdatePanel extends JPanel implements ActionListener {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    JPanel titlePanel;
    JLabel titleLable;
    JPanel dataPanel;//data Object may replace here
    JLabel jobTitle;
    JTextField jobTitleTextField;
    JLabel jobDesc;
    JTextField jobDescTextField;
    JLabel jobTechSkills;
    JTextArea jobTechSkillsTextField;
    JScrollPane jScrollPane1;
    JLabel jobMgmtSkills;
    JTextArea jobMgmtSkillsTextField;
    JScrollPane jScrollPane2;
    JLabel jobyearExp;
    JTextField jobyearExpTextField;
    JLabel maxSalary;
    JTextField maxSalaryTextField;
    JLabel minSalary;
    JTextField minSalaryTextField;
    JLabel startDate;
    JTextField startDateBox;
    JLabel jobOwner;
    JTextField jobOwnerTextField;
    JLabel compDesc;
    JTextField compDescTextField;
    JLabel postStatus;
    JComboBox statusBox;
    JLabel advCreateDate;
    JTextField advCreateDateBox;
    JLabel advModifyDate;
    JTextField advModifyDateBox;
    JPanel buttonPanel;
    JButton saveButton;
    JButton closeButton;
    JPanel allPanel;
    private String[] statuses = {"active", "in-active", "N/A"};
    final static boolean RIGHT_TO_LEFT = false;
    Toolkit tk = Toolkit.getDefaultToolkit();
    int xSize = ((int) tk.getScreenSize().getWidth());
    String datePattern = "dd/MM/yyyy";
    SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
    JobAdvertisement jobAdvert;
    private String jobAdvId;
    
   
   
    /**
     * Constructor for UpdatePanel
     * @param JobAdvertisement
     */   

    public UpdatePanel(JobAdvertisement adv) 
    {
    	try
    	{
    	jobAdvert=adv;
    	jobAdvId = jobAdvert.getId();
    	System.out.println("jobAdvId "+jobAdvId);
        titlePanel = new JPanel();
        titlePanel.setVisible(true);
        titlePanel.setPreferredSize(new Dimension(xSize - 430, 25));
        titlePanel.setBorder(
                new TitledBorder(new LineBorder(Color.GRAY, 1, false), "Add Job Advertisement Here", TitledBorder.LEFT, TitledBorder.BELOW_TOP));
        this.add(titlePanel, BorderLayout.NORTH);
        dataPanel = new JPanel();

        buttonPanel = new JPanel();

        saveButton = new JButton("Save");
        saveButton.addActionListener(this);
        buttonPanel.add(saveButton, BorderLayout.CENTER);
        
        closeButton = new JButton("Close");
        closeButton.addActionListener(this);
        buttonPanel.add(closeButton, BorderLayout.CENTER);

        //it may delete after adding setter getter methods;
        jobTitle = new JLabel("Job Title             :");
        jobDesc = new JLabel("Job Description       :");
        jobTechSkills = new JLabel("Technical Skills      :");
        jobMgmtSkills = new JLabel("Management Skills     :");
        jobyearExp = new JLabel("Year of Experience    :");
        maxSalary = new JLabel("Maximum Salary        :");
        minSalary = new JLabel("Minimum Salary        :");
        startDate = new JLabel("Start Date Joined     :");
        jobOwner = new JLabel("Owner                 :");
        compDesc = new JLabel("Company Description   :");
        postStatus = new JLabel("Status                :");
        advCreateDate = new JLabel("Created Date          :");
        advModifyDate = new JLabel("Modified Date         :");

        addComponentsToPane(new JPanel());

        this.add(allPanel);
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace();
    	}
    }

    public void addComponentsToPane(Container pane) throws Exception {

        if (RIGHT_TO_LEFT) {
            pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }

        pane.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        // first row below
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        pane.add(jobTitle, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 0;
        jobTitleTextField = new JTextField();
        if(jobAdvert.getTitle()!=null)
        	jobTitleTextField.setText(jobAdvert.getTitle());
        pane.add(jobTitleTextField, c);//data will come here by Object

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 0;
        pane.add(new JLabel(""), c);

        // second row below
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 1;
        
        pane.add(jobDesc, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 1;
        jobDescTextField = new JTextField();
        if(jobAdvert.getJobDescription()!=null)
        	jobDescTextField.setText(jobAdvert.getJobDescription());
        pane.add(jobDescTextField, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 1;
        pane.add(new JLabel(""), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 10;
        c.weightx = 0.0;
        c.gridx = 0;
        c.gridy = 2;
        pane.add(jobTechSkills, c);
        
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 10;
        c.weightx = 0.0;
        //c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 2;
        jobTechSkillsTextField = new JTextArea();//it may called up here
        jobTechSkillsTextField.setColumns(20);
        jobTechSkillsTextField.setLineWrap(true);
        jobTechSkillsTextField.setRows(3);
        jobTechSkillsTextField.setWrapStyleWord(true);
        if(jobAdvert.getTechnicalSkills()!=null)
        	jobTechSkillsTextField.setText(jobAdvert.getTechnicalSkills());
        jScrollPane1 = new JScrollPane(jobTechSkillsTextField);
        
        	
        pane.add(jScrollPane1, c);
        
        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 10;
        c.weightx = 0.0;
        // c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 2;
        pane.add(new JLabel(""), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 10;      //make this component tall
        c.weightx = 0.0;
        c.gridx = 0;
        c.gridy = 3;
        pane.add(jobMgmtSkills, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 10;
        c.weightx = 0.0;
        c.gridx = 1;
        c.gridy = 3;
        jobMgmtSkillsTextField = new JTextArea();
        jobMgmtSkillsTextField.setColumns(20);
        jobMgmtSkillsTextField.setLineWrap(true);
        jobMgmtSkillsTextField.setRows(3);
        jobMgmtSkillsTextField.setWrapStyleWord(true);
        jobMgmtSkillsTextField.setText(jobAdvert.getManagementSkills());
        jScrollPane2 = new JScrollPane(jobMgmtSkillsTextField);
        pane.add(jScrollPane2, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 10;
        c.weightx = 0.0;
        c.gridx = 2;
        c.gridy = 3;
        pane.add(new JLabel(""), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.gridx = 0;
        c.gridy = 4;
        pane.add(jobyearExp, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 4;
        jobyearExpTextField = new JTextField();
        if(jobAdvert!=null && jobAdvert.getRequiredWorkingExperience()>0)
        	jobyearExpTextField.setText(String.valueOf(jobAdvert.getRequiredWorkingExperience()));
        pane.add(jobyearExpTextField, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 4;
        pane.add(new JLabel(""), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.gridx = 0;
        c.gridy = 5;
        pane.add(maxSalary, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 0;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 5;
        maxSalaryTextField = new JTextField();
        if(jobAdvert!=null && jobAdvert.getSalaryMax()>0)
        	maxSalaryTextField.setText(String.valueOf(jobAdvert.getSalaryMax()));
        pane.add(maxSalaryTextField, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 5;
        pane.add(new JLabel(""), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 6;
        pane.add(minSalary, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 6;        
        minSalaryTextField = new JTextField();
        if(jobAdvert!=null && jobAdvert.getSalaryMin()>0)
        	minSalaryTextField.setText(String.valueOf(jobAdvert.getSalaryMin()));
        pane.add(minSalaryTextField, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 2;
        c.gridy = 6;
        pane.add(new JLabel(""), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 7;
        pane.add(startDate, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridwidth=GridBagConstraints.RELATIVE;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 7;

        startDateBox = new JTextField("DD/MM/YYYY");
        if(jobAdvert!=null && jobAdvert.getRequiredStartDate()!=null)
        {
            String dateStr = formatter.format(jobAdvert.getRequiredStartDate());
        	startDateBox.setText(dateStr);
        }
        Dimension zipSize = startDateBox.getPreferredSize();
        zipSize.width = 20;
        startDateBox.setPreferredSize(zipSize);
        pane.add(startDateBox, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 7;
        JLabel blunkLb = new JLabel("");
        blunkLb.setSize(500, 30);
        pane.add(blunkLb, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 8;
        pane.add(jobOwner, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 8;
        jobOwnerTextField = new JTextField();
        jobOwnerTextField.setText(jobAdvert.getOwner());
        pane.add(jobOwnerTextField, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 8;
        pane.add(new JLabel(""), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 9;
        pane.add(compDesc, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 9;
        compDescTextField = new JTextField();
        compDescTextField.setText(jobAdvert.getCompany());
        pane.add(compDescTextField, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 9;
        pane.add(new JLabel(""), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 10;
        pane.add(postStatus, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 10;
        statusBox = new JComboBox();
        statusBox.setPreferredSize(new Dimension(50, 30));
        for (int i = 0; i < statuses.length; i++) {
            statusBox.addItem(statuses[i]);
        }
        statusBox.setSelectedItem(jobAdvert.getStatus());
        pane.add(statusBox, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 10;
        pane.add(new JLabel(""), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 11;
        pane.add(advCreateDate, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 11;
        advCreateDateBox = new JTextField("DD/MM/YYYY", 10);          
        if(jobAdvert!=null && jobAdvert.getCreationDate()!=null)
        {
            String dateStr = formatter.format(jobAdvert.getCreationDate());
            advCreateDateBox.setText(dateStr);
            advCreateDateBox.setEditable(false);
        }
        pane.add(advCreateDateBox, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 11;
        pane.add(new JLabel(""), c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 12;
        pane.add(advModifyDate, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 1;
        c.gridy = 12;
        advModifyDateBox = new JTextField("DD/MM/YYYY");
        Date currentDate = new Date(System.currentTimeMillis());
        String updateDateStr = formatter.format(currentDate);
        advModifyDateBox.setText(updateDateStr);
        advModifyDateBox.setEditable(false);
        pane.add(advModifyDateBox, c);

        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 0.5;
        c.gridx = 0;
        c.gridy = 12;
        pane.add(new JLabel(""), c);

        pane.setVisible(true);
        pane.setPreferredSize(new Dimension(xSize - 500, 350));
        pane.setBackground(Color.GRAY);
        dataPanel.add(pane);

        allPanel = new JPanel(new BorderLayout());
        allPanel.add(titlePanel, BorderLayout.NORTH);
        allPanel.add(dataPanel, BorderLayout.CENTER);
        allPanel.add(buttonPanel, BorderLayout.SOUTH);

    }
    
    /**
     * Method to update JobAdvertisement    
     */
    public void saveAdvertisement()
    {
    	try
    	{
	    	JobAdvertisement job = new JobAdvertisement();
	    	System.out.println("===============Job Advertisment Id : " + jobAdvId);
	    	job.setId(jobAdvId);
	    	
	    	job.setCompany(compDescTextField.getText());;
  	
	    	job.setTitle(jobTitleTextField.getText());
	    	job.setJobDescription(jobDescTextField.getText());
	    	job.setTechnicalSkills(jobTechSkillsTextField.getText());
	    	job.setManagementSkills(jobMgmtSkillsTextField.getText());
	    	job.setRequiredWorkingExperience(Integer.parseInt(jobyearExpTextField.getText()));
	    	job.setSalaryMin(Integer.parseInt(minSalaryTextField.getText()));
	    	job.setSalaryMax(Integer.parseInt(maxSalaryTextField.getText()));
	    	job.setOwner(jobOwnerTextField.getText());
	    	job.setStatus(statusBox.getSelectedIndex());
	    	job.setRequiredStartDate((Date)formatter.parse(startDateBox.getText()));
	  		job.setPostingDate((Date)formatter.parse(advCreateDateBox.getText()));
	  		job.setCreationDate((Date)formatter.parse(advCreateDateBox.getText()));
	  		job.setModifyDate((Date)formatter.parse(advModifyDateBox.getText()));
	  		job.setExpiryDate((Date)formatter.parse(advModifyDateBox.getText()));
	  		
	  		
	  		System.out.println("Job Title .................. " + job.getTitle());
	  		JobAdvertisementController.getInstance().updateJobAdvertisement(job);
	  		JOptionPane.showMessageDialog(this,"Successfully save advertisement");
    	}
    	catch(Exception e)
    	{
    		e.printStackTrace();
    	}  		
    }

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}    	
}
